package com.dge.app.slice;

import com.dge.app.ResourceTable;
import com.dge.app.common.UserInfoMgr;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.webengine.JsCallback;
import ohos.agp.components.webengine.WebConfig;
import ohos.agp.components.webengine.WebView;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class WebViewAbilitySlice extends AbilitySlice {
    static final HiLogLabel label = new HiLogLabel(HiLog.DEBUG, 0x00201, "WebViewAbilitySlice"); // MY_MODULE=0x002
    public static final String EXTRA_URL = "extra_url";
    public static final String EXTRA_TITLE = "extra_title";
    private String url = "";
    private String title = "";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_webview);
        if (intent != null) {
            url = intent.getStringParam(EXTRA_URL);
            title = intent.getStringParam(EXTRA_TITLE);
        }
        initWebView();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initWebView() {
        WebView webView = findComponentById(ResourceTable.Id_webView);
        initViews(webView);
        WebConfig settings = webView.getWebConfig();
        settings.setWebStoragePermit(true);
        settings.setDataAbilityPermit(true);
        settings.setViewPortFitScreen(true);
        settings.setUserAgent(settings.getUserAgent() + " HarmonyOS");
        settings.setAutoFitOnLoad(true);
        // settings.setSupportZoom(false);
        settings.setLoadsImagesPermit(true);
        settings.setJavaScriptPermit(true);
        HiLog.debug(label, "addOverview userAgent = " + settings.getUserAgent());
        // webView.setWebAgent(new WebAgent() {
        //     @Override
        //     public void onLoadingPage(WebView webview, String url, PixelMap favicon) {
        //         super.onLoadingPage(webview, url, favicon);
        //         // 页面开始加载时自定义处理
        //     }
        //
        //     @Override
        //     public void onPageLoaded(WebView webview, String url) {
        //         super.onPageLoaded(webview, url);
        //         // 页面加载结束后自定义处理
        //     }
        //
        //     @Override
        //     public void onLoadingContent(WebView webview, String url) {
        //         super.onLoadingContent(webview, url);
        //         // 加载资源时自定义处理
        //     }
        //
        //     @Override
        //     public void onError(WebView webview, ResourceRequest request, ResourceError error) {
        //         super.onError(webview, request, error);
        //         // 发生错误时自定义处理
        //     }
        // });
        //
        // webView.setBrowserAgent(new BrowserAgent(this) {
        //     @Override
        //     public boolean onJsMessageShow(WebView webView, String url, String message, boolean isAlert, JsMessageResult result) {
        //         if (isAlert) {
        //             new ToastDialog(getApplicationContext()).setText(message).setAlignment(LayoutAlignment.CENTER).show();
        //             result.confirm();
        //             return true;
        //         } else {
        //             return super.onJsMessageShow(webView, url, message, isAlert, result);
        //         }
        //     }
        // });
        HiLog.debug(label, "url = " + url);
        if (url != null && !url.equals("")) {
            webView.load(url);
        }

    }

    private void initViews(WebView webView) {
        webView.addJsCallback("addApplication", new JsCallback() {
            @Override
            public String onCallback(String json) {
                HiLog.debug(label, "addApplication json = %s", json);
                if (json.startsWith("{")) {
                    // 是json
                    UserInfoMgr.statisticsData = json;
                    UserInfoMgr.saveUserInfo();
                    setResult(new Intent());
                }
                return "";
            }
        });

        webView.addJsCallback("addOverview", new JsCallback() {
            @Override
            public String onCallback(String json) {
                HiLog.debug(label, "addOverview json = %s", json);
                // 是json
                if (json.startsWith("{")) {
                    UserInfoMgr.statisticsData = json;
                    UserInfoMgr.saveUserInfo();
                    setResult(new Intent());
                }
                return "";
            }
        });

        webView.addJsCallback("getOverviews", new JsCallback() {
            @Override
            public String onCallback(String json) {
                HiLog.debug(label, "getOverview");
                return UserInfoMgr.statisticsData;
            }
        });

        webView.addJsCallback("getApplications", new JsCallback() {
            @Override
            public String onCallback(String json) {
                HiLog.debug(label, "getApplications");
                return UserInfoMgr.functionData;
            }
        });

        webView.addJsCallback("back", new JsCallback() {
            @Override
            public String onCallback(String json) {
                setResult(new Intent());
                terminate();
                return "";
            }
        });
    }
}
